package com.comment.validation;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class ArraySizeValidator implements ConstraintValidator<ArraySize, int[]> {

    private int min;
    private int max;

    @Override
    public void initialize(ArraySize constraintAnnotation) {
        this.min = constraintAnnotation.min();
        this.max = constraintAnnotation.max();
    }

    @Override
    public boolean isValid(int[] value, ConstraintValidatorContext context) {
        if (value == null) {
            return true; // 如果允许 null 值，可以返回 true；否则返回 false
        }
        int length = value.length;
        return length >= min && length <= max;
    }
}
